package com.huayue.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huayue.dao.*;
import com.huayue.entity.Depot;
import com.huayue.entity.DepotInOrder;
import com.huayue.entity.DepotInOrderDetails;
import com.huayue.entity.PurchaseOrder;
import com.huayue.util.PageHelper;

/**
 * 入库单
 */
@Service
public class DepotInOrderService extends BaseService<DepotInOrder> {

    @Autowired
    DepotInOrderDao dao;

    @Autowired
    DepotInOrderDetailsDao doddao;

    @Autowired
    PurchaseOrderDao podao;

    @Autowired
    PurchaseOrderDetailsDao poddao;

    @Autowired
    DepotDao ddao;

    @Override
    public BaseDao<DepotInOrder> getDao() {
        return dao;
    }

    /**
     * 查询最大的ID
     *
     * @return
     */
    public int findMaxId() {
        return dao.findMaxId();
    }

    /**
     * 查询未入库订单
     *
     * @param depot
     * @return
     */
    public Map<String, Object> findNotInDepotPurchase(DepotInOrder depot) {
        return dao.findNotInDepotPurchase(depot);
    }

    /**
     * 查询未入库订单详情
     *
     * @param depot
     * @return
     */
    public List<Map<String, Object>> findNotInDepotPurchaseDetails(DepotInOrder depot) {
        return dao.findNotInDepotPurchaseDetails(depot);
    }

    /**
     * 添加入库单信息
     *
     * @param orderInfo
     * @param details
     * @return
     */
    public boolean addDetails(DepotInOrder orderInfo, List<DepotInOrderDetails> details) {
        int success = 0;
        // 计算总件数和总金额
        int buyCount = 0;
        for (DepotInOrderDetails orderDetails : details) {
            buyCount += orderDetails.getNumber();
        }
        // 完善订单信息
        orderInfo.setSum(buyCount);
        // 将订单信息添加到数据库
        success = dao.insert(orderInfo);
        // 判断是否入库单添加成功
        if (success == 1) {
            // 将入库详情添加到数据库
            for (DepotInOrderDetails orderDetails : details) {
                success += doddao.insert(orderDetails);
            }
        }
        // 判断入库详情添加是否成功
        if (success == (details.size() + 1)) {
            PurchaseOrder po = new PurchaseOrder();
            po.setId(orderInfo.getPurchaseOrderId());
            po.setEmployee_id_retify(orderInfo.getEmployee_id_apply());
            // 判断是否添加了足量的商品（订货单详情，入库单详情对比，若入库单单个数量和订货单单个数量一样则为完整入库）
            // 获得进货单信息
            List<Map<String, Object>> pDetails = poddao.findDetails(String.valueOf(orderInfo.getPurchaseOrderId()));
            // 获得最新入库单的所有商品入库信息
            List<Map<String, Object>> nowInDetails = doddao.findDetailsByNowPurchaseOrder();
            int status = 3;
            for (Map<String, Object> map : pDetails) {
                for (Map<String, Object> nowMap : nowInDetails) {
                    if (map.get("product_id").equals(nowMap.get("product_id"))) {
                        int oldNumber = Integer.valueOf(map.get("number").toString());
                        int newNumber = Integer.valueOf(nowMap.get("sumnumber").toString());
                        if (oldNumber > newNumber) {
                            status = 2;
                            break;
                        }
                    }
                }
            }
            po.setIs_status(status);
            // 改变订货单状态
            success += podao.changeStatus(po);
            if (success == (details.size() + 2)) {
                // 将商品添加到库存中
                for (DepotInOrderDetails orderDetails : details) {
                    Depot depot = new Depot();
                    depot.setProduct_id(orderDetails.getProductId());
                    depot.setReal_number(orderDetails.getNumber());
                    success += ddao.addNumber(depot);
                }
                if (success == (details.size() * 2 + 2)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 查询已入库订单
     *
     * @param page
     * @param depotInOrder
     * @param objects
     * @return
     */
    public List<Map<String, Object>> findPageYes(PageHelper page, DepotInOrder depotInOrder, Object... objects) {
        return dao.findPageYes(page, depotInOrder, objects);
    }

    /**
     * 查询已入库订单总数
     *
     * @param objects
     * @return
     */
    public int countYes(Object... objects) {
        return dao.countYes(objects);
    }

    /**
     * 查询已入库的订单详情
     *
     * @param depot
     * @return
     */
    public List<Map<String, Object>> findYesInDepotPurchaseDetails(DepotInOrder depot) {
        return dao.findYesInDepotPurchaseDetails(depot);
    }
}